package com.nf.health.app.inquiry.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.inquiry.bean.ChangeFragmentBean;
import com.nf.health.app.inquiry.fragment.InquiryHaveCompletedFragment;
import com.nf.health.app.inquiry.fragment.InquiryNoCompleteFragment;
import com.nf.health.app.utils.ScreentUtils;
import com.nf.health.app.utils.ViewUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * @author Ian
 * @Description: 我的咨询界面 hepan 20160921
 * @date 2015-11-4 上午9:57:30
 */
public class MyInquiryActivity extends BaseActivity {
    /**
     * 记录按下的页签索引
     */
    private int index;
    //记录是否要返回我的界面
    public static String IS_GO_BACK_MINE = "is_go_back_mine";
    private boolean isGoBackToMain = false;
    private int currentTabIndex;
    private Fragment[] fragments;
    private View mIndicate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //逻辑比较简单,就不分开写initView initListener了
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_my_tiwen);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "问诊");
        isGoBackToMain = getIntent().getBooleanExtra(IS_GO_BACK_MINE, false);
        fragments = new Fragment[]{new InquiryNoCompleteFragment(), new InquiryHaveCompletedFragment()};
        getSupportFragmentManager().beginTransaction().add(R.id.ii_middle1, fragments[0]).show(fragments[0]).commit();
        RadioGroup rg_history = ViewUtil.getView(this, R.id.rg_history);
        mIndicate = ViewUtil.getView(this, R.id.view_indicate);
        changeIndicaterView(0);
        //切换fragment
        rg_history.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_no_complete:
                        index = 0;
                        break;
                    case R.id.rb_have_completed:
                        index = 1;
                        break;
                    default:
                        break;
                }
                if (currentTabIndex != index) {
                    changeIndicaterView(index);
                    FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
                    trx.hide(fragments[currentTabIndex]);
                    if (!fragments[index].isAdded()) {
                        trx.add(R.id.ii_middle1, fragments[index]);
                    }
                    trx.show(fragments[index]).commit();
                }
                currentTabIndex = index;
            }
        });
    }

    /**
     * 改变fragment指示横线,需求简单可以没用三方的,直接设置view位置来实现
     */
    private void changeIndicaterView(int index) {
        LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) mIndicate.getLayoutParams();
        p.width = ScreentUtils.getScreenWidth(this) / 2;
        if (0 == index) {
            p.leftMargin = 0;
        } else {
            p.leftMargin = ScreentUtils.getScreenWidth(this) / 2;
        }
        mIndicate.setLayoutParams(p);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isGoBackToMain){
            EventBus.getDefault().post(new ChangeFragmentBean(3));
        }
    }
}
